var Imported = Imported || {};
Imported.JIA_ItemMenuEvent = true;

var Wefjia = Wefjia || {};
Wefjia.ItemMenuEvent = Wefjia.ItemMenuEvent || {};
Wefjia.ItemMenuEvent.version = 2.1;

/*:
 * @plugindesc V2.10 使用带有公共事件效果的物品时不会再退出菜单了
 * @author Wefjia
 *
 * @help
 * 在拥有公共事件特性但不希望使用后退出菜单的物品备注中加入
 * <menuEvent> 标签
 * 
 * url: https://gitee.com/Alpha_fjh/mv-plugins;
 */

(function () {
	Wefjia.ItemMenuEvent.updateInterpreter = function (item) {
		this.interpreter.update();
		if (this.interpreter.isRunning()) {
			return;
		}
		if (this.interpreter.setupReservedCommonEvent(item)) {
			Wefjia.ItemMenuEvent.scene_Item.start();
		}
	}

	//=============================================================================
	// Menu_Interpreter
	//=============================================================================

	function Menu_Interpreter() {
		this.initialize.apply(this, arguments);
	}

	Menu_Interpreter.prototype = Object.create(Game_Interpreter.prototype);

	Menu_Interpreter.prototype.setupReservedCommonEvent = function (item) {
		if (item.meta.menuEvent && $gameTemp.isCommonEventReserved()) {
			Wefjia.ItemMenuEvent.scene_Item.stop();
			this.setup($gameTemp.reservedCommonEvent().list);
			$gameTemp.clearCommonEvent();
			return true;
		} else {
			return false;
		}
	}

	//=============================================================================
	// Scene_Item
	//=============================================================================

	Wefjia.ItemMenuEvent.Scene_item_create = Scene_Item.prototype.create;
	Scene_Item.prototype.create = function () {
		Wefjia.ItemMenuEvent.Scene_item_create.call(this);
		Wefjia.ItemMenuEvent.interpreter = Wefjia.ItemMenuEvent.interpreter || new Menu_Interpreter();
		this.createDialogWindow();
		Wefjia.ItemMenuEvent.scene_Item = this;
	}

	Scene_Item.prototype.useItem = function () {
		if (!Wefjia.ItemMenuEvent.dialog.isOpen()) Scene_ItemBase.prototype.useItem.call(this);
	}

	Scene_Item.prototype.checkCommonEvent = function () {
		if (!this.item().meta.menuEvent) Scene_ItemBase.prototype.checkCommonEvent.call(this)
	}

	Scene_Item.prototype.createDialogWindow = function () {
		Wefjia.ItemMenuEvent.dialog = Wefjia.ItemMenuEvent.dialog || new Window_Message();
		this.addWindow(Wefjia.ItemMenuEvent.dialog);
	}

	Scene_Item.prototype.update = function () {
		Scene_Base.prototype.update.call(this);
		if (this.isActive() && this.item()) Wefjia.ItemMenuEvent.updateInterpreter(this.item());
	}
})()
